/*
 * Copyright (c) 2015 4 17
 * Name:wuPeiJi
 * QQ:757671834
 */

package kali.wpj.kali.scan;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.marshalchen.common.commonUtils.urlUtils.HttpUtilsAsync;
import com.marshalchen.common.ui.ToastUtil;
import com.marshalchen.common.uimodule.slideactivity.SlidingActivity;

import org.apache.http.Header;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import kali.wpj.kali.R;
import kali.wpj.kali.globe.ServiceInfo;
import kali.wpj.kali.model.Port_info;
import kali.wpj.kali.util.JSONTools;

public class PortDetial_Activity extends SlidingActivity {
    private TextView port, service, other, advice, lv;
    private Port_info port_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_port_detial_);
        init();
        Intent intent = getIntent();
        final String port = intent.getStringExtra("port");
        new WebGetContnt().execute(ServiceInfo.searchPortInfo + port);

    }

    /**
     * &#x67e5;&#x8be2;&#x5177;&#x4f53;&#x7aef;&#x53e3;&#x7684;&#x4fe1;&#x606f;
     */

    public class WebGetContnt extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            URL url = null;//Java.net
            System.out.println("Address" + params[0]);
            try {
                url = new URL(params[0]);
                System.out.println("url" + url);

            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ByteArrayOutputStream data = null;
            try {

                HttpURLConnection connect = (HttpURLConnection) url.openConnection();
                connect.setRequestMethod("GET");
                connect.setReadTimeout(5000);
                InputStream inputStream = connect.getInputStream();
                data = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(buffer)) != -1) {
                    data.write(buffer, 0, len);
                }
                inputStream.close();
                return new String(data.toByteArray(), "UTF-8");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println(result);

            port_info = (Port_info) JSONTools.fromJson(result, Port_info.class);
            if (port == null) {
                ToastUtil.showLong(PortDetial_Activity.this, getString(R.string.sorryNetW));
                return;
            }
            port.append(port_info.getPort());
            service.append(port_info.getService());
            other.append(port_info.getOther());
            advice.append(port_info.getAdvice());
            lv.append(port_info.getLv() + "");
        }

    }


    void init() {
        port = (TextView) findViewById(R.id.port);
        service = (TextView) findViewById(R.id.service);
        other = (TextView) findViewById(R.id.other);
        advice = (TextView) findViewById(R.id.advice);
        lv = (TextView) findViewById(R.id.lv);
    }

}
